<?php



include '/simplehtmldom/simple_html_dom.php';
set_time_limit(0);

header('connection', 'Keep-Alive');
header('accept-language', 'en-us');
header('accept-encoding', 'gzip, deflate');
header('user-agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)'); 

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('connected to DB not successfully !!!' . mysql_error());
}
echo 'connected to DB successfully <br>';

$db_selected = mysql_select_db('bcc', $link);
if (!$db_selected) {
    die ('No DB available : ' . mysql_error());
}

$handle = fopen ("output.txt", w);
		



for($id = 57690 ; $id <= 66000;$id++){;
	
		
	$html = file_get_html('http://www.billycurrington.com/member/friends.aspx?mid=' . $id);
	
	$name = $html->find('h1', 1);
	$name = explode("&rsquo;",$name->plaintext);
	$username = trim($name[0]);
	
	print_out( "\n" . $id . ";		$username;		",$handle);
	
	
	if( $html->find("span[class=userimage]") ||  $html->find("div[class=noContent]")){
		
		
		$sql="INSERT INTO chars (datasetId, url, site, rank, title) 
		  VALUES ('1', 
				  'http://www.billycurrington.com/member/friends.aspx?mid=" . $id . "',
				  '" . $id ."',
				  0,
				  '". $username . "')";
		$result = mysql_query($sql);
		
		print_out( "User inserted;		",$handle);

		
		if($html->find("div[class=noContent]")){
			print_out( "no Friends;		",$handle);
		}else{
			$i = 0;
			foreach($html->find("span[class=userimage]") as $e){
				foreach($e->find("a") as $a){
					$e = explode("?", $a->href );				
					$sql="INSERT INTO links (datasetId, link_from, link_to) 
					  VALUES ('1', 
							  'http://www.billycurrington.com/member/friends.aspx?mid=" . $id . "',
							  'http://www.billycurrington.com/member/friends.aspx?" . $e[1] ."')";

					$result = mysql_query($sql);
					$i++;
				}
			};
		print_out( $i . " Friends;		",$handle);
		}

	}else{
		print_out("User not existing; 	",$handle);
	}
	$r = rand(50, 100);
	if($id % $r == 0){
		sleep(rand(1, 2));
	}
	unset($html);
	unset($result);
	unset($name);
	unset($username);
	unset($sql);
	unset($e);
	unset($i);
}

mysql_close($link);	
fclose ($handle);




function print_out($string, $handle){
	echo $string;
	fwrite ($handle, $string);
}


?>